var app = getApp();
var request = app.request;
var common = require('../../../utils/common.js');
Page({
	data: {
    statusBar: app.globalData.statusBar,
		url: app.globalData.setting.url,
		resourceUrl: app.globalData.setting.resourceUrl,
		requestData: null,
		binding: false, // 是否绑定手机号
		login_mobile: false, //是否显示验证码登录
		wap_home_logo: '', //商城logo
	},

	onLoad: function() {
		var that = this;
		app.getConfig(function(res) {
			var is_bind_account = common.getConfigByName(res.config, 'is_bind_account');
			if (is_bind_account == 1) {
				that.setData({
					login_mobile: true
				})
			}
			var wap_login_logo = common.getConfigByName(res.config, 'wap_login_logo');
			if (wap_login_logo) {
				that.setData({
					wap_login_logo: ((wap_login_logo.indexOf('http') > 0) ? '' : that.data
						.url) + wap_login_logo
				})
			}
		});
		wx.getSystemInfo({
			success(res) {
				that.setData({
					windowWidth: res.windowWidth,
					windowHeight: res.windowHeight,
				})
			}
		})
	},

	//返回的时候也可以刷新
	onShow: function() {

	},
	getPhoneNumber: function(e) {
		this.setData({
			binding: false
    })
    console.log(e);
		if (e.detail.errMsg == 'getPhoneNumber:ok') {
			var iv = e.detail.iv;
			var code = app.globalData.code;
			var encryptedData = e.detail.encryptedData;
			app.request.post('/api/user/bindMobile', {
				data: {
					code: code,
					encryptedData: encryptedData,
					iv: iv,
				},
				successReload: true,
				isShowLoading: false,
				success: function(res) {
					if (res.data.status == 1) {
						app.showSuccess('手机号授权成功', function() {
							wx.setStorageSync('isAuth', true);
							wx.setStorageSync('app:userInfo', res.data.result.user);
							app.globalData.userInfo = res.data.result.user;
							app.globalData.userInfo.head_pic = common.getFullUrl(app
								.globalData.userInfo.head_pic);
							wx.removeStorageSync('bind_third_login');
							wx.navigateBack()
						});
					} else {
						app.showWarning(res.data.msg);
					}
				},
			});
		} else if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
			app.showWarning("请绑定手机号");
		} else {
      app.showWarning("获取手机号失败");
    }
	},

	bindGetUserinfo: function(res) {
		var that = this;
		if (res.detail.userInfo != undefined) {
			try {
				wx.setStorageSync('wx_user_info', res.detail);
				app.globalData.wechatUser = res.detail.userInfo;
				app.auth.checkLogin(app.globalData.code, res.detail, function(loginData) {
					if (typeof loginData == 'undefined') {
						that.setData({
							binding: true
						})
					} else {
						app.getUserInfo(function(userInfo) {
							app.globalData.userInfo = userInfo
							app.showSuccess('登录成功', function() {
								wx.removeStorageSync('first_leader');
								wx.removeStorageSync('unique_id');
								wx.navigateBack();
							});
						}, true, false);

					}
				});
			} catch (e) {
				console.log(e);
			}
		} else {
			console.log('bindGetUserinfo fail . res.detail.userInfo is undefined');
		}
	},
	// 手机号登录  
	moblie_login: function() {
		wx.navigateTo({
			url: '/pages/user/moblie_login/moblie_login',
		})
	},
	// 取消绑定
	cancel: function() {
		app.auth.clearAuth();
		this.setData({
			binding: false
		})
  },
  // 返回上一页
  back() {
    wx.reLaunch({
			url: '/pages/index/index/index',
		})
  }
});